package com.starwish.starwishblog.web.back;


import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Board;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.BoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin/board")
@Api(value = "对板块的操作")
@Log4j2
public class BoardController {
    @Autowired
    BoardService boardService;
    @PostMapping("/addBoard")
    @ApiOperation(value = "添加板块!")
    public ResultResponse<Void> addBoard(@RequestBody Board board){
        ResultResponse<Void> result = null;
        try {
            board.setCreateTime(new Date());
            int i = boardService.addBoards(board);
            result = new ResultResponse<>(200,"添加板块成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加板块失败!");
        }
        return result;
    }

    @DeleteMapping("/deleteBoard/{id}")
    @ApiOperation(value = "删除板块!")
    public ResultResponse<Void> deleteBoard(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = boardService.removeBoard(id);
            result = new ResultResponse<>(200,"删除板块成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除板块失败!");
        }
        return result;
    }


    @GetMapping("/hotBoard")
    @ApiOperation(value = "热门/取消热门板块!")
    public ResultResponse<Void> hotBoard(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        ResultResponse<Void> result = null;
        try {
            int i = boardService.hotBoardById(id,status);
            result = new ResultResponse<>(200,"推荐热门成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"推荐热门失败!");
        }
        return result;
    }

    @PutMapping("/modifyBoard")
    @ApiOperation(value = "修改板块信息!")
    public ResultResponse<Void> modifyBoard(@RequestBody Board board){
        ResultResponse<Void> result = null;
        try {
            board.setModifyTime(new Date());
            int i = boardService.modify(board);
            result = new ResultResponse<>(200,"修改板块成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改板块失败!");
        }
        return result;
    }

    @GetMapping("/showBoardByPage")
    @ApiOperation(value = "分页查询板块!")
    public ResultResponse<PageInfo<Board>> showBoardByPage(@RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size, String name){
        ResultResponse<PageInfo<Board>> result = null;
        try {
            PageInfo<Board> pageInfo = boardService.queryBoardByPage(now, size, name);
            result = new ResultResponse<>(200,"查询板块信息成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询板块信息失败");
        }
        return result;
    }

    @GetMapping("/showALLBoard")
    @ApiOperation(value = "查询所有板块!")
    public ResultResponse<List<Board>> showALLBoard(){
        ResultResponse<List<Board>> result = null;
        try {
            List<Board> boards = boardService.findAllBoard();
            result = new ResultResponse<>(200,"查询所有板块成功",boards);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(200,"查询所有板块成功");
        }
        return result;
    }


}
